<?php

 class Reservas
 {
     private static $instancia = null;

     private static $id;
     private static $tabla=__class__;
     private static $pks;
     private static $pk;
     private static $debug;

     private $data;

     private function __construct()
     {
         //print_r($this->data);
     }

     static public function getInstancia($id,$debug = false)
     {

         if (!self::$instancia instanceof self)
         {
             self::$instancia = new self;
             self::$id = $id;
             self::$tabla = strtolower(self::$tabla);
/*             self::$tabla = $tabla;
             self::$pk = $pk;*/
             self::$debug = $debug;
             self::$instancia->init(self::$id,self::$debug);
         }
          self::$instancia->init($id,self::$debug);
         return self::$instancia;
     }

     private function init($id, $debug)
     {
         if (isset($id, $debug))
         {
             $db = Zen::Db($debug); //instanciar la DB
             $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
              $pks = $db->MetaPrimaryKeys(self::$tabla);
             self::$pk = $pks[0];
             $sql = "SELECT * FROM " . self::$tabla . " WHERE " . self::$pk . "='$id'";
            
             //$sql = "SELECT * FROM reservas WHERE res_id='$id'";
             $this->data = $db->GetAll($db->Prepare($sql));
             return true;
         } else
         {
             return false;
         }

     }
     public function Datos()
     {
        return $this->data[0];

     }
     public function __get($property)
     {

         if (isset($this->data[0][$property]))
         {

             return Zen::Capitalizar($this->data[0][$property]);

         } else
         {

             //return false;
             //return '<strong>No existe el atributo ' . $property . '</strong>';
              return 'Sin Datos';

         }

     }


     public function __set($property, $value)
     {
          //$db = Zen::Db($debug); //instanciar la DB
          $reg=array();
          $reg[$property]=$value;
          $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
          $res=Zen::AutoSql(self::$debug,'reservas',$reg,'UPDATE',"res_id='".self::$id."'",false,false);
          if($res)
          {
            self::init(self::$id,self::$debug);
            return true;
          }
          else
          {
            return false;
          }
     }

     public function __toString()
     {

         return 'No se permite.';

     }
     public function __clone()
     {
         trigger_error("Operación Invalida: No puedes clonar una instancia de " .
             get_class($this) . " class.", E_USER_ERROR);
     }
     public function __wakeup()
     {
         trigger_error("No puedes deserializar una instancia de " . get_class($this) .
             " class.");
     }

 }

?>